
import java.awt.Container;
import java.io.Serializable;
import javax.swing.JButton;

/**
 *
 * @author anderzanic
 */
public class Partidas extends javax.swing.JDialog implements Serializable {

    private static Partidas partidas;
    private Login login;
    private Jogador jogador;
    private Jogo jogo;

    /**
     * Creates new form Partidas
     */
    public static Partidas getInstance(java.awt.Frame login, boolean modal, Jogo jogo, Jogador jogador) {
        if (!partidaCompleta(jogo)) {
            partidas = new Partidas(login, modal, jogo, jogador);
        }
        
        return partidas;
    }

    public Partidas(java.awt.Frame login, boolean modal, Jogo jogo, Jogador jogador) {
        super(login, modal);
        initComponents();
        
        this.setLocationRelativeTo(null);
        this.jogo = jogo;
        this.login = (Login) login;
        this.jogador = jogador;
        
        addJogador(jogador);
        this.setVisible(true);
    }

    private static boolean partidaCompleta(Jogo jogo) {
        return jogo.esteJogoCompleto();
    }
    
    private void addJogador(Jogador jogador){
        
        Container pane = this.getContentPane();
        pane.setLayout(null);
        JButton button = new JButton(jogador.getLogin());
        button.setVisible(true);
        pane.add(button);
        button.setBounds(10,10, (jogador.getLogin().length() * 20),40);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnFechar = new javax.swing.JButton();
        btnDesistir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(400, 350));
        setMinimumSize(new java.awt.Dimension(400, 350));
        getContentPane().setLayout(null);

        btnFechar.setText("Fechar");
        btnFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFecharActionPerformed(evt);
            }
        });
        getContentPane().add(btnFechar);
        btnFechar.setBounds(260, 280, 120, 29);

        btnDesistir.setText("Desistir");
        btnDesistir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDesistirActionPerformed(evt);
            }
        });
        getContentPane().add(btnDesistir);
        btnDesistir.setBounds(120, 280, 140, 29);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnDesistirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDesistirActionPerformed

        System.exit(0);
    }//GEN-LAST:event_btnDesistirActionPerformed

    private void btnFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFecharActionPerformed
        this.jogo.removeJogador(jogador);
        this.login.setVisible(true);
        close();
    }//GEN-LAST:event_btnFecharActionPerformed

    private void close() {
        dispose();
    }
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(Partidas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(Partidas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(Partidas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(Partidas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the dialog */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                Partidas dialog = new Partidas(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDesistir;
    private javax.swing.JButton btnFechar;
    // End of variables declaration//GEN-END:variables
}
